package com.rxt.thermopro.ui.fragment

import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.widget.EditText
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.viewmodel.DeviceStatusViewModel
import kotlinx.android.synthetic.main.fragment_device_setting.*
import kotlinx.android.synthetic.main.fragment_device_setting.backButton
import kotlinx.android.synthetic.main.fragment_device_setting.linear1

class DeviceSettingFragment : BaseFragment<BaseViewModel>() {

    override fun layoutId(): Int {
        return R.layout.fragment_device_setting
    }

    companion object {
        private const val ARG_DEVICE_NAME = "deviceName"
        private const val ARG_DEVICE_ADDRESS = "address"

        fun newInstance(): DeviceSettingFragment {
            return DeviceSettingFragment()
        }

        fun newInstance(deviceName: String, address: String): DeviceSettingFragment {
            return DeviceSettingFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_DEVICE_NAME, deviceName)
                    putString(ARG_DEVICE_ADDRESS, address)
                }
            }
        }
    }


    private lateinit var mDeviceStatusViewModel: DeviceStatusViewModel
    private var deviceName = ""
    private var address = ""

    override fun initView(savedInstanceState: Bundle?) {
        mDeviceStatusViewModel =
            ViewModelProvider(requireActivity()).get(DeviceStatusViewModel::class.java)
        deviceName = arguments?.getString(ARG_DEVICE_NAME, "") ?: ""
        address = arguments?.getString(ARG_DEVICE_ADDRESS, "") ?: ""

        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        linear3.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    FAQSFragment.newInstance()
                )
                .addToBackStack("main")
                .commit()
        }

        linear1.setOnClickListener {
            val parentView = LinearLayout(requireContext())
            parentView.gravity = Gravity.CENTER
            val editText = EditText(requireContext())
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            params.marginStart = 32
            params.marginEnd = 32
            editText.hint = deviceName
            parentView.addView(editText, params)
            val dialog = AlertDialog.Builder(requireContext())
                .setTitle(R.string.text_device_name)
                .setView(parentView)
                .setNegativeButton(
                    R.string.text_Cancel
                ) { _, _ ->

                }
                .setPositiveButton(R.string.text_yes) { _, _ ->

                }
                .show()
            dialog.getButton(DialogInterface.BUTTON_POSITIVE)
                .setTextColor(resources.getColor(R.color.purple_200))
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
                .setTextColor(resources.getColor(R.color.gray_text))
        }

        viewDeleteBBQ.setOnClickListener {
            val dialog = AlertDialog.Builder(requireContext())
                .setMessage(R.string.text_sure_delete_device)
                .setNegativeButton(
                    R.string.text_Cancel
                ) { _, _ ->

                }
                .setPositiveButton(R.string.text_yes) { _, _ ->
                    requireActivity().onBackPressed()
                    mDeviceStatusViewModel.removeDevice.value = hashMapOf<Long, String>().apply {
                        this[System.currentTimeMillis()] = address
                    }
                }
                .show()
            dialog.getButton(DialogInterface.BUTTON_POSITIVE)
                .setTextColor(resources.getColor(R.color.gray_text))
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
                .setTextColor(resources.getColor(R.color.gray_text))
        }

        linear2.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    TP359AlertSettingFragment.newInstance(address)
                )
                .addToBackStack("main")
                .commit()
        }
    }

}